package meb

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mosongcc/gosdk/weixin_oauth"
	"github.com/mosongcc/gotool/gobjectid"
	"time"
	"vitepress-serve/internal/common/hgin"
)

// OauthAuthorize 获取授权链接
func (ctl *Controller) OauthAuthorize(ctx *gin.Context) (err error) {
	var (
		args  = hgin.ApiReq[OauthAuthorizeArgs](ctx)
		state = gobjectid.Hex()
	)
	ctl.cache.Set("origin"+state, args.Origin, 3*time.Minute)
	body := &OauthAuthorizeBody{
		State: state,
		Url:   fmt.Sprintf("https://%s%s?scope=%s&state=%s", ctx.Request.Host, PATH_WEIXIN_AUTHORIZE, weixin_oauth.SnsapiUserinfo, state),
	}
	hgin.ApiRes(ctx, body)
	return
}

type OauthAuthorizeArgs struct {
	Origin string `json:"origin"` //来源页面链接，授权完成后跳转
}

type OauthAuthorizeBody struct {
	State string `json:"state"` //授权码
	Url   string `json:"url"`   //授权链接
}
